<!DOCTYPE HTML>
<html>

<head>
    <title>proton.js-behaviour-repulsion</title>
    <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta charset="utf-8">
    <style type="text/css">
        body {
            background-color: #333333;
            margin: 0px;
            overflow: hidden;
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            -khtml-user-select: none;
            user-select: none;
        }
        
        #testCanvas {
            background: #000;
        }
    </style>
</head>

<body>
    <canvas id="testCanvas"></canvas>
    <script src="../../lib/requestAnimationFrame.min.js"></script>
    <script src="../../lib/stats.min.js"></script>
    <script src="//localhost:3001/build/proton.js"></script>
    <script>
        var canvas;
        var context;
        var proton;
        var renderer;
        var emitter;
        var stats;
        var mouseObj;
        var attractionBehaviour, repulsionBehaviour, crossZoneBehaviour;

        Main();

        function Main() {
            canvas = document.getElementById("testCanvas");
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            addStats();

            createProton();
            createRenderer();
            tick();
            
            canvas.addEventListener('mousedown', mousedownHandler, false);
            canvas.addEventListener('mouseup', mouseupHandler, false);
            canvas.addEventListener('mousemove', mousemoveHandler, false);
            
            window.onresize = function(e) {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
                crossZoneBehaviour.reset(new Proton.RectZone(0, 0, canvas.width, canvas.height), 'cross');
            }
        }

        function addStats() {
            stats = new Stats();
            stats.setMode(2);
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';
            document.body.appendChild(stats.domElement);
        }

        function createProton() {
            proton = new Proton;
            
            emitter = new Proton.Emitter();
            emitter.damping = 0.0075;
            emitter.rate = new Proton.Rate(600);
            
            emitter.addInitialize(new Proton.Body('image/star.gif', 128, 128));
            emitter.addInitialize(new Proton.Mass(1), new Proton.Radius(Proton.getSpan(5, 10)));
            emitter.addInitialize(new Proton.Velocity(new Proton.Span(1, 3), new Proton.Span(0, 360), 'polar'));

            mouseObj = {
                x: canvas.width / 2,
                y: canvas.height / 2
            };
            attractionBehaviour = new Proton.Attraction(mouseObj, 20, 1300);
            repulsionBehaviour = new Proton.Repulsion(mouseObj, 30, 350);
            crossZoneBehaviour = new Proton.CrossZone(new Proton.RectZone(0, 0, canvas.width, canvas.height), 'cross');
            emitter.addBehaviour(attractionBehaviour, repulsionBehaviour, crossZoneBehaviour, new Proton.Color('random'));
            emitter.addBehaviour(new Proton.Scale(Proton.getSpan(.1, .7)));
            emitter.addBehaviour(new Proton.Rotate(Proton.getSpan(0, 180), Proton.getSpan(-5, 5), 'add'));
            
            emitter.p.x = canvas.width / 2;
            emitter.p.y = canvas.height / 2;
            emitter.emit('once');
            proton.addEmitter(emitter);
        }

        function mousedownHandler(e) {
            attractionBehaviour.reset(mouseObj, 0, 0);
        }

        function mouseupHandler(e) {
            attractionBehaviour.reset(mouseObj, 20, 1300);
        }

        function mousemoveHandler(e) {
            if (e.layerX || e.layerX == 0) {
                mouseObj.x = e.layerX;
                mouseObj.y = e.layerY;
            } else if (e.offsetX || e.offsetX == 0) {
                mouseObj.x = e.offsetX;
                mouseObj.y = e.offsetY;
            }
        }

        function createRenderer() {
            renderer = new Proton.WebGLRenderer(canvas);
            renderer.blendFunc("SRC_ALPHA", "ONE")
            proton.addRenderer(renderer);
        }

        function tick() {
            requestAnimationFrame(tick);

            stats.begin();
            proton.update();
            stats.end();
        }
    </script>
</body>

</html>